package com.koron.operation.classes.shift.bean.dto;

import com.koron.operation.classes.schedule.bean.dto.ClassesShiftDTO;
import com.koron.operation.classes.schedule.bean.po.ClassesStaff;
import com.koron.operation.classes.shift.bean.po.ClassesShiftRecord;
import com.koron.operation.classes.shift.bean.po.ClassesTip;
import com.koron.operation.classes.shift.bean.po.ClassesWorkCompletion;
import com.koron.operation.classes.shift.bean.query.WorkFeignQuery;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交接班记录详情
 *
 * @author jzp
 * @since 2021/10/28
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ShiftRecordDTO extends ClassesShiftRecord {

    /**
     * 排班方案id
     */
    @ApiModelProperty("排班方案名称")
    private String scheduleName;


    /**
     * 值班人员类型，数据字典：operation_staff_type
     */
    @ApiModelProperty("值班人员类型，数据字典：operation_staff_type")
    private String staffType;

    /**
     * 值班单位id
     */
    @ApiModelProperty("值班单位id")
    private String deptId;

    /**
     * 值班单位名称
     */
    @ApiModelProperty("值班单位名称")
    private String deptName;


    /**
     * 值班地点id
     */
    @ApiModelProperty("值班地点id")
    private String locationId;

    /**
     * 值班地点名称
     */
    @ApiModelProperty("值班地点名称")
    private String locationName;

    @ApiModelProperty("本值班次")
    private ClassesShiftDTO thisShift;

    @ApiModelProperty("下值班次")
    private ClassesShiftDTO nextShift;

    @ApiModelProperty("工作完成情况")
    private List<ClassesWorkCompletion> workCompletions;

    @ApiModelProperty("交接事项")
    private List<ClassesTip> tips;

    @ApiModelProperty("提前交班时间戳")
    private Long advanceTime;

    public WorkFeignQuery extractWorkFeignQuery() {
        WorkFeignQuery workFeignQuery = new WorkFeignQuery();
        workFeignQuery.setDeptId(this.deptId);
        workFeignQuery.setLocationId(this.locationId);
        workFeignQuery.setStaffType(this.staffType);
        ClassesShiftDTO shift = new ClassesShiftDTO();
        /*
         * 如果本值是值班中，则查本值
         * 如果本值是已交班或者下值是未接班，则查下值
         */
        if (thisShift.getStatus() == 1) {
            shift = thisShift;
        } else if (thisShift.getStatus() == 2) {
            shift = nextShift;
        }
        workFeignQuery.setDates(Arrays.asList(shift.getStartTime(), shift.getEndTime()));
        if (!CollectionUtils.isEmpty(shift.getStaffs())) {
            workFeignQuery.setStaffs(shift.getStaffs().stream().map(ClassesStaff::getStaffBy).collect(Collectors.toList()));
        }
        return workFeignQuery;
    }
}
